//
//  FWPageView.swift
//  DBS
//
//  Created by 王方伟 on 2017/4/18.
//  Copyright © 2017年 wfw. All rights reserved.
//

import UIKit

class FWPageView: UIView {
    fileprivate var titles : [String]
    fileprivate var childVCs : [UIViewController]
    fileprivate var parentVC : UIViewController
    fileprivate var style : FWTitleStyle
    fileprivate var titleView : FWTitleView!
    fileprivate var contentView : FWContentView!

    init(frame: CGRect, titles:[String], childVCs:[UIViewController], parentVC:UIViewController, style:FWTitleStyle) {
        self.titles = titles
        self.childVCs = childVCs
        self.parentVC = parentVC
        self.style = style
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}


//MARK:设置UI
extension FWPageView {
    //设置头部的title
    fileprivate func setupUI(){
        setupTitleView()
        setupContentView()
    }
    
    private func setupTitleView(){
        let frame = CGRect(x: 0, y: 0, width: bounds.width, height: style.titleHeight)
        titleView = FWTitleView(frame: frame, titles: titles, style:style)
        titleView.backgroundColor = style.backageGroudColor
        titleView.delegate = self;
        addSubview(titleView)
    }
    
    private func setupContentView(){
        let frame = CGRect(x: 0, y: style.titleHeight, width: bounds.width, height: bounds.height - style.titleHeight)
        contentView = FWContentView(frame: frame, childVCs: childVCs, parentVC: parentVC)
        contentView.delegate = self
        addSubview(contentView)
    }
}

extension FWPageView : TitleViewDelegate {
    func onClickTitleView(_ titleView: FWTitleView, from: NSInteger, to: NSInteger) {
        contentView.setCurrentIndex(to)
    }

}

extension FWPageView : ContentViewDelegate {
    func onSlideContentView(_ contentView: FWContentView, from: NSInteger, to: NSInteger, progress: CGFloat) {
        titleView.onSlideContentView(from: from, to: to, progress: progress)
    }
    
    func contentViewEndScroll(_ contentView: FWContentView) {
        titleView.contentViewDidEndScroll()
    }
}
